// miniprogram/pages/redeem/success.js
Page({
  data: {
    item: {},
    couponCode: '',
    expireDate: ''
  },

  onLoad(options) {
    if (options.itemId) {
      this.loadRedeemItem(options.itemId)
    }
    
    // 生成模拟券码和过期时间
    this.setData({
      couponCode: 'ST' + Date.now().toString().substr(-8),
      expireDate: this.getExpireDate(30)
    })
  },

  loadRedeemItem(id) {
    // 模拟加载兑换商品信息
    const item = {
      id: id,
      name: '星巴克咖啡券',
      image: '/images/redeem_starbucks.png'
    }
    
    this.setData({ item })
  },

  getExpireDate(days) {
    const date = new Date()
    date.setDate(date.getDate() + days)
    return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
  },

  copyCouponCode() {
    wx.setClipboardData({
      data: this.data.couponCode,
      success: () => {
        wx.showToast({
          title: '券码已复制',
          icon: 'success'
        })
      }
    })
  },

  goBack() {
    wx.redirectTo({
      url: '/pages/redeem/index'
    })
  },

  viewRedeemHistory() {
    wx.redirectTo({
      url: '/pages/redeem/history'
    })
  }
})